<template>
	<view class="page">
		<view class="address-input">
			<view class="list-input">
				<view class="title">
					<text>收货人</text>
				</view>
				<view class="content">
					<input v-model="info.uname" type="text" placeholder="请填写收货人姓名">
				</view>
			</view>
			<view class="list-input">
				<view class="title">
					<text>手机号</text>
				</view>
				<view class="content">
					<input v-model="info.mobile" type="tel" placeholder="请填写收货人手机号">
				</view>
			</view>
			<view class="list-input">
				<view class="title">
					<text>所在地区</text>
				</view>
				<view class="content">
					<AddressPicker @change="change" :level="3">
						<view v-if="!info.province">
							请选择地址
						</view>
						<view v-else>
							{{info.province+info.city+info.district}}
						</view>
					</AddressPicker>
				</view>
			</view>
			<view class="list-textarea">
				<view class="title">
					<text>详细地址</text>
				</view>
				<view class="content">
					<textarea v-model="info.address" type="tel" placeholder="街道/楼牌号等" />
				</view>
			</view>
		</view>
		<view class="tag-default">
		<!-- 	<view class="tag-list">
				<view class="title">
					<text>标签</text>
				</view>
				<view class="content">
					<view class="list action">
						<text>家</text>
					</view>
					<view class="list">
						<text>公司</text>
					</view>
					<view class="list">
						<text>学校</text>
					</view>
				</view>
			</view> -->
			<view class="default-address">
				<view class="title">
					<text>默认地址</text>
				</view>
				<view class="switch-default">
					<!-- <switch class="red sm" color="#0077EE !important" checked></switch> -->
					<u-switch v-model="value" activeColor="#d0abf4" @change="changeSwitch"></u-switch>

				</view>
			</view>
		</view>
		<view class="footer-btn">
			<view class="btn" @click="keepMess">
				<text>{{typeb==0?'保存':'修改'}}</text>
			</view>
		</view>
	</view>
</template>
<script>
	
	import AddressPicker from "@/components/address-picker/AddressPicker.vue"
	export default {
		components:{AddressPicker},
		data() {
			return {
				value:true,
				planLocate:'请选择地址',
				addressType: '2',
				info:{
					is_default:false,
					province:'',
					city:'',
					district:''
				},
				is_loading:false,
				isShowCity:false,
				status:false,
				type:0,
				address:'',
				typeb:0,
				listAddress:[]
			};
		},
		onLoad(options) {
			console.log(options,888888888)
			// this.addressType = options.type||'2';
			let _address = JSON.parse(decodeURIComponent(options.item))
			this.typeb = options.typeb
			this.info = _address
			this.info.is_default = _address.is_default==1?true:false
			if(this.typeb==2){
				
				this.planLocate = _address.address
				uni.setNavigationBarTitle({
					title:'编辑收货地址'
				})
			}
			
		},
		methods:{
			// 默认监听
			changeSwitch(value){
				console.log(value)
			},
			checkingNull(){
				if (!this.info.uname) {
					uni.showToast({
						icon: 'none',
						title: '请输入收货人姓名'
					})
					return false
				}
				if (!this.info.mobile) {
					uni.showToast({
						icon: 'none',
						title: '请输入收货人手机号码'
					})
					return false
				}
				if (!this.$u.test.mobile(this.info.mobile)) {
					uni.showToast({
						icon: 'none',
						title: '您输入的手机号码有误'
					})
					return false
				}
				if (!this.info.province) {
					uni.showToast({
						icon: 'none',
						title: '请选择所在地区'
					})
					return false
				}
				if (!this.info.address) {
					uni.showToast({
						icon: 'none',
						title: '请输入详细地址'
					})
					return false
				}
				return true
			},
			keepMess(){
				if(!this.checkingNull()) return
				if(this.typeb==2){
					this.info.is_default = this.value
					this.exchangeAddress()
					return false
				}
				this.info.is_default = this.value
				this.$u.api.addressAdd({
					uname:this.info.uname,
					mobile:this.info.mobile,
					province:this.info.province,
					city:this.info.city,
					district:this.info.district,
					address:this.info.address,
					is_default:this.info.is_default,
				})
				.then(res=>{
					if(res.status){
						this.info.id = res.msg
						uni.showToast({
							icon: 'none',
							title: '地址添加成功'
						})
						setTimeout(()=>{
							if(this.type ==1){
								uni.$emit('changeaddress',{msg:encodeURIComponent(JSON.stringify(this.info))})
								uni.navigateBack()
								return
							}
							uni.navigateBack()
						},500)
						return
					}
					uni.showToast({
						icon: 'none',
						title: res.msg
					})
				})
				.catch(err=>{
					console.log(err);
					uni.showToast({
						icon: 'none',
						title: "网络异常，请稍后再试"
					})
				})
			},
			// 修改
			exchangeAddress(){
				if(!this.checkingNull()) return
					this.is_default = true
					console.log(this.info);
					this.$u.api.addressEdit(this.info)
					.then(res=>{
						console.log(res)
						if(res.status){
							uni.showToast({
								icon: 'none',
								title: res.msg
							})
							setTimeout(()=>{
								uni.navigateBack()
							},500)
							return
						}
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					})
					.catch(err=>{
						console.log(err);
						uni.showToast({
							icon: 'none',
							title: "网络异常，请稍后再试"
						})
					})
			},
			//父组件的获取子组件传递的值
			change(result) {
				 
				  this.planLocate = ''; 
				  result.data.forEach( (item,index) =>{
					  if(index===0){
						   this.planLocate += item.name
					  }else{
						  this.planLocate += '-' +item.name  
					  }
				  }) ;
				  console.log(result,this.planLocate,'arr2')
				  // this.info.address = this.planLocate
				  
				  let arr =this.planLocate.split('-');
				  this.listAddress = arr
				  this.info.province =arr[0]
				  this.info.city =arr[1]
				  this.info.district =arr[2]
				  console.log(arr,'arr')
			},
		}
	}
</script>

<style scoped lang="scss">
	@import 'AddressEdit.scss';
</style>
